package com.weixin;

import com.weixin.config.WeiXinProperties;
import com.weixin.config.aes.AesException;
import com.weixin.config.aes.WXBizMsgCrypt;
import com.weixin.util.CSVUtils;
import org.junit.Test;
import org.springframework.beans.propertyeditors.URLEditor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/1414:51
 */
public class XmlEncryptMsg {

    @Test
    public void encrypt() throws AesException {
        WXBizMsgCrypt wxcpt = new WXBizMsgCrypt("gB8OwG6NaP9tRVHhKWL3I", "rp0OvIjqTNln4zDEiTlPZJT2SP2ZRJIKxQ7lYF6jJ4P","ww4141aaf47adf8dd5");
        String xml = "<xml> \n" +
                "   <ToUserName><![CDATA[tyojko]]></ToUserName>\n" +
                "   <AgentID><![CDATA[10002]]></AgentID>\n" +
                "   <Encrypt><![CDATA[1234]]></Encrypt>\n" +
                "</xml>\n";
        String sEncryptMsg = wxcpt.EncryptMsg(xml, "1618372948", "ps3lgxf8l9");
        System.out.println(sEncryptMsg);
    }


    @Test
    public void Url() throws UnsupportedEncodingException {
       // String url="http://callback.tococn.com:9001/v1.0/wei-xin/cgi-bin/query?id=1";
        String url="http://callback.tococn.com:9001/v1.0/wei-xin/cgi-bin/login";

        String utf8 = URLEncoder.encode(url, "UTF8");
        System.out.println(utf8);
        String utf81 = URLDecoder.decode(utf8, "UTF8");
        System.out.println(utf81);
    }

    @Test
    public void fie() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("static/batch_user_sample.csv");

        File file1 = new File(classPathResource.getURI().getPath());

        FileInputStream inputStream = new FileInputStream(file1);
        MultipartFile upfile = new MockMultipartFile(file1.getName(), inputStream);
        System.out.println(upfile);
    }

    @Test
    public void getPartyListFile() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("static");
        File f=new File(classPathResource.getURI().getPath());
        if (!f.exists()) {
            System.out.println(classPathResource.getURI().getPath() + " not exists");
            return;
        }else {
            File fa[] = f.listFiles();
            if(fa.length==0){
                return;
            }
            for (File  file: fa) {
                if (file.getName().equals("batch_party_sample.csv")){
                    file.delete();
                }
            }
        }

        List exportData = new ArrayList<Map>();
        Map row1 = new LinkedHashMap<String, String>();
        row1.put("name", "腾道");
        row1.put("id", "1");
        row1.put("parentid", "0");
        row1.put("order", "1");
        exportData.add(row1);
        row1 = new LinkedHashMap<String, String>();
        row1.put("name", "总办");
        row1.put("id", "2");
        row1.put("parentid", "1");
        row1.put("order", "1");
        exportData.add(row1);
        row1 = new LinkedHashMap<String, String>();
        row1.put("name", "微信事业群");
        row1.put("id", "3");
        row1.put("parentid", "1");
        row1.put("order", "2");
        exportData.add(row1);
        row1 = new LinkedHashMap<String, String>();
        row1.put("name", "产品部");
        row1.put("id", "4");
        row1.put("parentid", "3");
        row1.put("order", "1");
        exportData.add(row1);
        LinkedHashMap map = new LinkedHashMap();
        map.put("name", "部门名称");
        map.put("id", "部门ID");
        map.put("parentid", "父部门ID");
        map.put("order", "排序");

        String fileName = "batch_party_sample";
        File file = CSVUtils.createCSVFile(exportData, map, classPathResource.getURL().getPath(), fileName);
        String fileName2 = file.getName();
        System.out.println("文件名称：" + fileName2);
    }
}
